"""
读取数据库中的数据到文件
"""
from pymysql import connect, cursors

conn = connect(
    host='localhost',
    user='root',
    password='111246',
    autocommit=True,
)
cursor = conn.cursor()
conn.select_db("py_sql")
cursor.execute('select * from orders')
result = cursor.fetchall()
# for row in result:
#     print(row)
data_dict = {}
f = open('data.txt', 'w', encoding='utf-8')
for row in result:
    data_dict["date"] = row[0]
    data_dict["order_id"] = row[1]
    data_dict["money"] = row[2]
    data_dict["province"] = row[3]
    f.write(str(data_dict) + '\n')
f.close()
cursor.close()
conn.close()